<?php if(!defined('ARCH_ROOT_PATH')) exit;
/**
 *	Architect Framework
 *
 *	Architect Framework is a light weight object oriented web applications framework written for PHP 5.1.4 and later that provide the tools to develop and deploy a website or application fast and with as little code as possible.
 *
 *	@author Robin Grass, <hello@coffinboy.se>
 *	@link http://architect.kodlabbet.net
 *	@license http://www.opensource.org/licenses/gpl-3.0.html GNU General Public License (v. 3)
 */

/**
 *	Architect_Database
 *
 *	Simple database wrapper for PDO.
 *
 *	@package Utilities
 *	@version 1.0
 *	@author Robin Grass, <hello@coffinboy.se>
 */
class Architect_Database extends PDO {

	/**
	 *	@var string $dsn
	 */
	private $dsn;

	/**
	 *	@var array $drivers
	 */
	protected $drivers = array(
		'pgsql' 		=> 'host=$host port=$port dbname=$dbname',
		'sqlite' 		=> null,
		'mysql' 		=> 'host=$host;port=$port;dbname=$dbname',
		'firebird' 	=> 'Database=$dbname;DataSource=$host;Port=$port',
		'informix' 	=> null,
		'oracle' 		=> null,
		'odbc' 			=> null,
		'dblib' 		=> 'host=$host:$port;dbname=$dbname',
		'ibm' 			=> null
	);
	
	/**
	 *	@var array $stored_queries
	 */
	protected $stored_queries = array();
	
	/**
	 *	__construct
	 *
	 *	@param string $driver
	 *	@param string $database
	 *	@param string $host
	 *	@param int $host
	 */
	public function __construct($driver, $database, $host, $port = false) {
		if(!array_key_exists($driver, $this->drivers))
			throw new Architect_Exception('Database driver does not exist!');
		
		if($this->drivers[$driver] === null)
			throw new Architect_Exception('Database driver not supported.');
		
		if($driver == 'dblib' && !$port)
			throw new Architect_Exception('Database DBLIB needs a port.');
			
		$this->setDataSourceName($driver, $database, $host, $port);
	}
	
	/**
	 *	setDataSourceName
	 *
	 *	@param string $driver
	 *	@param string $database
	 *	@param string $host
	 *	@param int $host
	 */
	protected function setDataSourceName($driver, $database, $host, $port = false) {
		$this->dsn = $driver . ':' . $this->drivers[$driver];
		
		if($port === false)
			$this->dsn = preg_replace('/(port)\=\$(port)([;| ])/i', '', $this->dsn);
		else $this->dsn = str_ireplace('$port', $port, $this->dsn);
		
		$this->dsn = str_ireplace(array('$dbname', '$host'), array($database, $host), $this->dsn);
	}
	
	/**
	 *	getDataSourceName
	 *
	 *	@return string $dsn
	 */
	protected function getDataSourceName() {
		return $this->dsn;
	}
	
	/**
	 *	connect
	 *
	 *	@param string $user
	 *	@param string $pass
	 */
	public function connect($user, $pass) {
		parent::__construct($this->getDataSourceName(), $user, $pass);
	}
	
	/**
	 *	storeQuery
	 *
	 *	This method stores a query to be retrieved later
	 *
	 *	@param string $id
	 *	@param string $statement
	 */
	public function storeQuery($id, $statement) {
		$this->stored_queries[$id] = $statement;
	}
	
	/**
	 *	retrieveQuery
	 *
	 *	This method stores a query to be retrieved later
	 *
	 *	@param string $id
	 *	@param string $statement
	 */
	public function retrieveQuery($id) {
		if(array_key_exists($id, $this->stored_queries));
			return $this->stored_queries[$id];
	}

}
?>